package com.keyingbo.leran.pattern.adapter;

import com.keyingbo.leran.pattern.adapter.oldservice.ExpByHardCodeService;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: KEYB
 * @Date: 2019-3-19 00:05
 * @Description:
 */
public class DemoServiceAdapter implements ExpByAnnotationService{
    private ExpByHardCodeService demoService;

    public DemoServiceAdapter(ExpByHardCodeService demoService) {
        this.demoService = demoService;
    }

    @Override
    public void expData(List<?> list) {
        if (list == null || list.size() == 0) return;
        //1.获取对象实例
        Object o = list.get(0);
        //2.通过注解获取Object各字段的属性构建head
        Map<String,String> head = new HashMap<>();
        for (Field field : o.getClass().getFields()) {
            Exp exp = field.getAnnotation(Exp.class);
            if (exp != null){
                head.put(exp.key(),exp.name());
            }
        }
        //3.构建List《Map》数据值
        List<Map<String,Object>> dataMap = new ArrayList<>();
        ////for o  list 此处省略处理
        //4.执行原方法导出
        demoService.expData(head,dataMap);
    }
}
